import {api} from './api'
import { apiHttp } from '__service/csHttp'
import Vue from 'vue'
import store from '../store';
import csConst from '../store/csConst';
// 'use strict'
/*
 *添加小红点
 *
 * */
export async function addRemind({ dataId, dataTab, dataType }) {
    let query = {
        dataId, // 业务id
        dataTab, // tab页顺序	string	@mock=7
        dataType, // 1拟投资项目,2已投资项目,3母基金,4拟设立基金,5已设立基金,6客户,7潜在项目
    };
    await apiHttp(api.REMIND_ADD, query);
    store.dispatch('getMsg');
}
/*
 *获取详情页红点
 *
 * */
export async function getDetailRemind({ dataId, dataType }) {
    let query = {
        dataId, // 业务id
        dataType, // 1拟投资项目,2已投资项目,3母基金,4拟设立基金,5已设立基金,6客户,7潜在项目
    };
    const { data } = await apiHttp(api.REMIND_GET_DETAIL, query);
    return data;
}
/*
 *取消详情页红点
 *
 * */
export async function read({ dataId, dataTab, dataType }) {
    let query = {
        dataId, // 业务id
        dataTab, // tab页顺序	string	@mock=7
        dataType, // 1拟投资项目,2已投资项目,3母基金,4拟设立基金,5已设立基金,6客户,7潜在项目
    };
    await apiHttp(api.REMIND_READ, query);
    store.dispatch('getMsg');
}
// 数组去重
export function arrayUnique(array) {
    let set = new Set(array);
    return Array.from(set)
}
// 判断权限 menuList中 type=3时 代表权限
export function isNoAuth(authName) {
    let menuList = store.state.user.menuList || [];
    let authList = [];
    const loop = (list, name = '') => {
        list.map((item) => {
            if (item.type === 3) {
                authList.push(name + item.name);
            }
            if (item.childMenus && item.childMenus.length > 0) {
                item.childMenus.map((x, i) => {
                    if (x.type === 3) {
                        authList.push(name + item.name + '-' + x.name);
                    }
                    if (x.childMenus && x.childMenus.length > 0 && i === 0) {
                        // console.log(x, 'x>>>>>', item)
                        loop(item.childMenus, (item.name + '-'));
                    }
                })
            }
        })
    };
    // if (authName === '我的首页-查看') {
    //     console.log(authList, 'authList>>>');
    // }
    loop(menuList);
    if (!csConst.state.FOF) {
        authName = authName.replace('拟投资基金', '拟设立基金');
        authName = authName.replace('已投资基金', '已设立基金');
    }
    return authList.indexOf(authName) !== -1;
}

// 图片、txt 预览 传入 id  新建一个弹框 显示图片 txt   type: img   txt
export const openImgInBody = ( id, type ) => {
    const promise = new Promise(function () {
        const MyModal = Vue.extend({});
        const modal = new MyModal({
            template: `<el-dialog :title="title" :visible.sync="dialogVisible">
                            <div class="ca-p-20 ca-align-center"
                                 style="min-height:300px;"   
                                 v-if="type == 'img'">
                                <img :src="imgSrc" width="80%">
                            </div>
                            <div v-else style="min-height:300px;">
                                {{txt}}
                            </div>
                        </el-dialog>`,
            data() {
                return {
                    dialogVisible: true,
                    imgSrc: this.baseURL + '/file/' + id + '?X-Token=' + store.state.user.token,
                    type: type, // img or  txt
                    title: type === 'img' ? '图片预览' : 'txt预览',
                    txt: ''
                }
            },
            watch: {
            },
            mounted() {
                if (type === 'txt') {
                    this.getDoc();
                }
            },
            methods: {
                async getDoc() {
                    let params = {
                        id: id
                    };
                    this.txt = await apiHttp(api.GET_TXT_DOC, params);
                }
            },
        });
        // 或者，在文档之外渲染并且随后挂载
        const component = modal.$mount();
        document.body.appendChild(component.$el);
    });
    return promise
}

